/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#ifndef INCLUDE_NET_NETINSIDE_NETBUFFER_H_
#define INCLUDE_NET_NETINSIDE_NETBUFFER_H_

#include <deque>
#include "base/xrefptr.h"
#include "base/xguard.h"
#include "base/xsingleton.h"
#include "netdata.h"
#include "net/netbasedefine.h"


class net_buffer_pool_t;
class net_buffer_row_t : public smart::refobj_t
{
	friend class net_buffer_pool_t;
public:
	//int m_row_id;
	SOCK_FD m_fd;
	netdata_t m_data;

	char m_ip[64];
	int m_port;

protected:
	net_buffer_row_t();
};
typedef std::deque< smart::ref_ptr<net_buffer_row_t> > NetBufferRowQueue;


#define MAX_BUFFER_BLOCK    16*1024    //16KB
class net_buffer_pool_t : public smart::singleton_t<net_buffer_pool_t>
{
	friend class smart::singleton_t<net_buffer_pool_t>;
public:
	net_buffer_pool_t();
	~net_buffer_pool_t();

	void set_pool_threshold( int threshold );

	smart::ref_ptr<net_buffer_row_t> create_buffer_row();
	int recycle( smart::ref_ptr<net_buffer_row_t>& ref_row );

protected:
	NetBufferRowQueue m_buffer_pool;
	smart::mutex_t m_lock;
	int m_threshold; //default size=10000
};

#endif //~INCLUDE_NET_NETINSIDE_NETBUFFER_H_
